package com.hhly.rabbitmq.delay;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.hhly.rabbitmq.RabbitMQUtil;
import com.rabbitmq.client.Channel;

public class Producer {
	
	private static String queue_name = "delay.queue"; 
	 
    public static void main(String[] args) throws IOException, InterruptedException {
    	 Channel channel = RabbitMQUtil.getChannel();
    	 //设置延迟过期后投递的交换机
    	 channel.exchangeDeclare("delay.exchange", "fanout");
    	 //消息发送交换机
    	 channel.exchangeDeclare("delaysync.exchange", "direct", false, false, false, null);
    	 //延迟过期列队
    	 Map<String, Object> map = new HashMap<>();
    	 map.put("x-message-ttl", 5000);
    	 map.put("x-max-length", 5000000);
    	 map.put("x-dead-letter-exchange", "delay.exchange");//绑定消息过期投递的交换机
    	 channel.queueDeclare(queue_name, true, false, false, map);
    	 //绑定延迟过期列队到发送消息交换机
    	 channel.queueBind(queue_name, "delaysync.exchange", "deal");
    	 int i = 0;
    	 for(;;){
    		 Thread.sleep(1000);
    		 String message = "hello world!"+i++ +"|"+ System.currentTimeMillis();  
             channel.basicPublish("delaysync.exchange", "deal", null, message.getBytes());  
             System.out.println(message);
    	 }
         
	}
}
